/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD;

import java.util.ArrayList;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Tom McNair - S41758984
 * @author David Bullock - 42062222
 */
public class Question {

    protected final Log logger = LogFactory.getLog(getClass());
    protected Integer id;
    private String text = null;
    /**
     * 0=multiple choice (MC) or 1 = short response(SR)
     */
    private int questionType = -1;
    /**
     * 0 means pending, 1 means open, 2 means closed.
     * 0 by default
     */
    private int status = 0;
    /**
     * the ID of the parent, -1 means not yet set
     */
    private int pollId = -1;
    Date creationDate;
    boolean deleted;
    boolean overwritten;
    private boolean isPublic;

    public Question() {
    }

    /**
     * get the parent Poll ID
     * @return the parent Poll ID
     */
    public int getPollId() {
        return pollId;
    }

    /**
     * set the parent Poll ID
     * @param pollId parent Poll ID
     */
    public void setPollId(int pollId) {
        this.pollId = pollId;
    }

    public int getId() {
        return id;
    }

    public void setId(int questionID) {
        this.id = questionID;
    }

    public String getText() {
        return text;
    }

    public void setText(String questionText) {
        this.text = questionText;
    }

    /**
     * Will return the status of the question. The first letter will be capitalized
     * @return the status of the question.
     */
    public String getStatus() {
        switch (status) {
            case 0:
                return "Pending";
            case 1:
                return "Open";
            case 2:
                return "Close";
            default:
                logger.info("Shouldn't have gotten here. ERROR!!!");
                logger.info("Starting skynet...");
                return null;
        }
    }

    /**
     * Only used for validation. Will return if the question is pending
     * @return True if the question is pending. False otherwise
     */
    public boolean isPending() {
        if (status == 0) {
            return true;
        }
        return false;
    }

    /**
     * Only used for validation. Will return if the question is open
     * @return True if the question is open. False otherwise
     */
    public boolean isOpen() {
        if (status == 1) {
            return true;
        }
        return false;

    }

    /**
     * Only used for validation. Will return if the question is closed
     * @return True if the question is closed. False otherwise
     */
    public boolean isClosed() {
        if (status == 2) {
            return true;
        }
        return false;

    }

    /**
     * Sets the status of the question.
     * @param status may be either "closed", "pending", or "open". Case does not matter
     * @return True if setting it was successful. False if invalid input was given
     */
    public boolean setStatus(int status) {
        if (status == 0) {
            this.status = 0;
        } else if (status == 1) {
            this.status = 1;
        } else if (status == 2) {
            this.status = 2;
        } else {
            return false;
        }
        return true;
    }

    public void setPending() {
        this.status = 0;
    }

    public void setOpen() {
        this.status = 1;
    }

    public void setClosed() {
        this.status = 2;
    }

    /**
     * Specifies if this is a multiple choice  ("MC") or short response("SR")
     * question
     * @return MC = multiple choice, SR = Short Response, null= Not yet set
     */
    public String getType() {
        return (questionType==0)?"MC":"SR";
    }

    public void setMC() {
        this.questionType = 0;
    }

    public void setSR() {
        this.questionType = 1;
    }
    
    public boolean isMC(){
        return (questionType==0)? true:false;
    }
    
    public boolean isSR(){
        return (questionType==1)? true:false;
    }    
    

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public boolean isOverwritten() {
        return overwritten;
    }

    public void setOverwritten(boolean overwritten) {
        this.overwritten = overwritten;
    }
    
    public boolean isPublic(){
        return isPublic;
    }

    public void setPublic(boolean isPublic) {
        this.isPublic = isPublic;
    }
    
    
    

    public ArrayList<Question> genTestQuestion(int seed) {
        ArrayList<Question> q = new ArrayList<Question>();
        int x = 0;
        for ( int i = 0; i < 10; i++) {
            for (int j = 0; j < 3; j++) {
                if ((j % 2) == 0) {
                    q.add(new MCQuestion(new MCAnswer().genTest(i)));                    
                    //q.get(x).setAnswers(new MCAnswer().genTestAnswer(x));
                    q.get(x).setPublic(true);
                } else {
                    q.add(new SRQuestion());
                    q.get(x).setPublic(false);
                }
                q.get(x).setId(x);
                q.get(x).setText("Poll " + seed + " Question " + x);
                q.get(x).setPending();

                if ((j % 3) == 0) {
                    q.get(x).setOpen();
                } else if ((j % 2) == 0) {
                    q.get(x).setClosed();
                } else {
                    q.get(x).setPending();
                }                
                x++;
            }
        }
        return q;
    }
}
